import app_config as config
from .file_gen import *
from .hadoop_gen import getZookeeperIds


class HBaseSiteCreator(FileCreator):
    def getFileName(self):
        return 'hbase-site.xml'

    def initData(self):
        self.data['hbase_home'] = config.hadoop_config.get('home')
        self.data['zk-ids'] = getZookeeperIds()


class HBaseEnvCreator(FileCreator):
    def getFileName(self):
        return 'hbase-env.sh'

    def initData(self):
        self.data['java_home'] = config.java_config.get('home')


class BackupMasterCreator(FileCreator):
    def getFileName(self):
        return 'backup-masters'

    def initData(self):
        masters = config.hbase_config.get('master_nodes')

        backups = []
        for i in range(1, len(masters)):
            backups.append(masters[i].get('host'))

        self.data['backup-masters'] = '\n'.join(backups)


class RegionServersCreator(FileCreator):
    def getFileName(self):
        return 'regionservers'

    def initData(self):
        regions = config.hbase_config.get('region_nodes')

        hosts = []
        for item in regions:
            hosts.append(item.get('host'))

        self.data['regionservers'] = '\n'.join(hosts)
        pass


class HBaseConfigManager(FileManager):
    def init(self):
        self.creators = [
            HBaseSiteCreator(self.importDir, self.exportDir),
            HBaseEnvCreator(self.importDir, self.exportDir),
            BackupMasterCreator(self.importDir, self.exportDir),
            RegionServersCreator(self.importDir, self.exportDir)
        ]
